import util from '@/libs/util.js'

export const state = () => ({
  token: null,
  user:{
    avatar: '',
    username: '',
    role: 0
  },
  siteInfo:{
    logo: '',
    webName: '房车新旅程',
    ICP:'',
    copyright:'',
    version: '',
    techSupport:''
  }
})

export const mutations = {
  setGlobalToken(state, token) {
    state.token = token
  },
  setGlobalUserInfo(state, userInfo) {
    state.user.avatar = userInfo.avatar
    state.user.username = userInfo.username
    state.user.role = userInfo.role
  },
  setGlobalSiteInfo(state, siteInfo) {
    state.siteInfo.logo = siteInfo.logo
    state.siteInfo.webName = siteInfo.webName
    state.siteInfo.ICP = siteInfo.ICP
    state.siteInfo.copyright = siteInfo.copyright
    state.siteInfo.version = siteInfo.version
  }
}

export const actions = {
  async nuxtServerInit({ dispatch, commit }, { req, res, $api }) {

    //获取网站信息
    /*
    let response = await $api.post('/web/public/info/website', {}, true)
    if (response.meta.code==200) {
      commit('setGlobalSiteInfo', response.data)
    }
    */
   
    //获取token
    let token = util.getCookieInServer(req, 'token')
    commit('setGlobalToken', token)

    //获取用户信息
    let userInfo = util.getCookieInServer(req, 'userInfo')
    if (userInfo) {
      userInfo = decodeURIComponent(userInfo)
      commit('setGlobalUserInfo', JSON.parse(userInfo))
    }

  }
}
